package com.zeyi.blog.social.gtiee;

import com.zeyi.blog.entity.User;
import com.zeyi.blog.service.UserService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

/**
 * @author 黎先森
 */
public class GiteeAuthProvider implements AuthenticationProvider {

    private UserService userService;

    public GiteeAuthProvider(UserService userService) {
        this.userService = userService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        GiteeAuthToken authToken = (GiteeAuthToken) authentication;
        GiteeUser giteeUser = (GiteeUser) authToken.getPrincipal();
        User user = userService.thirdLogin(giteeUser);
        return new GiteeAuthToken(user);
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return GiteeAuthToken.class.isAssignableFrom(aClass);
    }
}
